﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Contracts.Exceptions;

namespace XcoAppSpaces.Transport.WCF
{
	/// <summary>
	/// Configuration string interpreter for the XcoWCFTransportService
	/// </summary>
    public class ConfigStringInterpreter : XcoConfigStringInterpreterBase
	{
        /// <summary>
        /// Configuration key for setting the port.
        /// </summary>
        public const string ConfigPort = "wcf.port";
        /// <summary>
        /// Configuration key for setting the name.
        /// </summary>
        public const string ConfigName = "wcf.name";
        /// <summary>
        /// Configuration key for setting if the service is the default communication service.
        /// </summary>
        public const string ConfigIsDefault = "wcf.isdefault";

	    
	    private int? _port;
	    private string _name;
        private bool? _isdefault;

        /// <summary>
        /// Action definition for interpretion
        /// </summary>
        public override Dictionary<string, Action<string>> InterpretDefinition
        {
            get
            {
                return new Dictionary<string, Action<string>> 
                    { 
                        {ConfigPort, AnalysePort},//Configuration key for setting the port.
                        {ConfigName, AnalyseName},//Configuration key for setting the name.
                        {ConfigIsDefault, AnalyseIsDefault},//Configuration key for setting if the service is the default communication service.
                    };
            }
        }

        /// <summary>
        /// Get the current WCF service
        /// </summary>
        public override IXcoService GetService
        {
            get
            {
                if (_port.HasValue || _name!= null || _isdefault.HasValue)
                {
                    if (_name!= null)
                    {
                        ServiceConfig.Name = _name;
                    }
                    if (_isdefault.HasValue )
                    {
                        ServiceConfig.IsDefault = _isdefault.Value;
                    }
                    return _port.HasValue
                               ? new XcoWCFTransportService(_port.Value)
                               : new XcoWCFTransportService();
                }
                return null;
            }
        }

        #region analyse methods
        void AnalysePort(string portStr)
        {            
            //try to load port-definition from the configuration
            int tmpPort;
            if (int.TryParse(portStr, out tmpPort))
            {
                _port = tmpPort;
            }
            else
            {
                throw new XcoConfigurationException("Port must be a number!");
            }
        }

        void AnalyseName(string name)
        {
            _name = name;
        }

        void AnalyseIsDefault(string isDefaultStr)
        {
            bool tmpIsDefault;
            if (bool.TryParse(isDefaultStr, out tmpIsDefault))
            {                //try to load the isDefault-definition from the configuration
                _isdefault = tmpIsDefault;
            }
            else
            {
                throw new XcoConfigurationException("IsDefault must be either \"true\" or \"false\"!");
            }
        }
        #endregion
    }
}
